#include<iostream>
#include<string>
#include<vector>
#include<string.h>

using std::endl;
using std::cout;
using std::string;
using std::vector;

//定义person，名字、年龄、提供信息输出函数
class Person
{
public:
    
    Person(const char* name,int age)
        :_name(new char[strlen(name)+1]())
        ,_age(age)
    {
        cout<<"基类有参构造"<<endl;
        strcpy(_name,name);
    }

    ~Person()
    {
        if(_name)
        {
            delete []_name;
            _name=nullptr;
            cout<<"基类析构delete空间"<<endl;
        }
    }

    //拷贝构造
    Person(const Person& rhs)
        :_name(new char[strlen(rhs._name)+1]())
         ,_age(rhs._age)
    {
        cout<<"基类的拷贝构造"<<endl;
        strcpy(_name,rhs._name);
    }

    //三合成原理,赋值运算函数
    Person& operator=(const Person& lhs)
    {
        //非自赋值
        if(this!=&lhs)
        {
            cout<<"真正赋值运算"<<endl;
            delete []_name;
            _name=new char[strlen(lhs._name)+1]();
            strcpy(_name,lhs._name);
            _age=lhs._age;
        }
        return *this;
    }
    void display()
    {
        cout<<"name: "<<_name<<endl;
        cout<<"age: "<<_age<<endl;
    }
private:

    char* _name;
    int _age;
};
//派生处Employee 包含部门(char*),salary ,display输出所有，要求可以复制、赋值、可以计算平均工资
class Employee
:public Person
{
public:
    Employee(const char* name,int age,const char* partment,double salary)
        :Person(name,age)
         ,_depart(new char [strlen(partment)+1]())
         ,_salary(salary)
    {
        cout<<"派生有参构造"<<endl;
        strcpy(_depart,partment);
    }
    ~Employee()
    {
        if(_depart)
        {
            delete []_depart;
            cout<<"派生析构delete 空间"<<endl;
        }
    }
    friend
double CountAvgSalary(const vector<Employee>& arr);

    Employee(const Employee& lhs)
        :Person(lhs)
         ,_depart(new char[strlen(lhs._depart)+1]())
         ,_salary(lhs._salary)
    {
        cout<<"派生拷贝构造"<<endl;
        strcpy(_depart,lhs._depart);
    }

    Employee& operator=(const Employee& lhs)
    {
        //赋值运算函数在需要显式调用
        if(this!=&lhs)
        {
        Person::operator=(lhs);
            delete []_depart;
            _depart=new char [strlen(lhs._depart)+1]();
            strcpy(_depart,lhs._depart);
            _salary=lhs._salary;
        }
        return *this;
    }

    void display()
    {
        cout<<"基类隐藏"<<endl;
        Person::display();
        cout<<"partment: "<<_depart<<endl
            <<"salary: "<<_salary<<endl;
    }
    //除此之外需要进行手动拷贝构造以及赋值运算

private:
char* _depart;
double _salary;
};

double CountAvgSalary(const vector<Employee>& arr)
{
    double all=0;
    if(arr.size()==0)
    {
        return 0;
    }
    for(auto&i:arr)
    {
        all +=i._salary;
    }
    return all/arr.size();
}
int main(void)
{
    vector<Employee> arr;
    Employee a("x",18,"math",666);
    Employee b("yt",18,"math",123);
    Employee c("uh",18,"math",321);
    Employee d("xs",18,"math",145);
    Employee e("wa",18,"math",234);
    Employee f("aa",18,"math",123);
    a.display();
    f=e=d=c=b=a;
    arr.push_back(a);
    arr.push_back(b);
    arr.push_back(c);
    arr.push_back(d);
    arr.push_back(e);
    arr.push_back(f);
    cout<<CountAvgSalary(arr);
}


